<template>
  <view class="user-profile">
    <view class="profile-header">
      <image class="avatar" :src="userInfo.avatarUrl"></image>
      <view class="nickname">{{userInfo.nickName}}</view>
    </view>
    <view class="profile-content">
      <view class="stats-panel">
        <view class="stat-item" v-for="(item, index) in statsData" :key="index">
          <text class="stat-value">{{ item.count }}</text>
          <text>{{ item.title }}</text>
        </view>
      </view>
      <view class="order-panel">
        <view class="panel-title">我的订单</view>
        <view class="order-list">
          <view class="order-item" v-for="(item, index) in orderTypes" :key="index">
            <image class="order-icon" :src="item.image"></image>
            <text class="order-name">{{ item.title }}</text>
          </view>
        </view>
      </view>
      <view class="menu-panel">
        <view class="menu-item" v-for="(item, index) in menuItems" :key="index" @click="handleMenuItemClick(item.title)">
          <text>{{ item.title }}</text>
          <uni-icons type="right" size="18"></uni-icons>
        </view>
      </view>
      <!-- 提示窗示例 -->
      <uni-popup ref="alertDialog" type="dialog">
        <uni-popup-dialog type="warn" cancelText="取消" confirmText="确认" title="温馨提示" content="是否要退出登录？" @confirm="dialogConfirm">
        </uni-popup-dialog>
      </uni-popup>
    </view>
  </view>
</template>

<script>
  import { mapState, mapMutations } from 'vuex'
  export default {
    name:"my-userInfo",
    computed: {
      ...mapState('m_user', ['userInfo'])
    },
    data() {
      return {
        statsData: [
          { count: 8, title: '收藏的店铺' },
          { count: 14, title: '收藏的商品' },
          { count: 18, title: '关注的商品' },
          { count: 84, title: '足迹' }
        ],
        orderTypes: [
          { image: '/static/my-icons/icon1.png', title: '待付款' },
          { image: '/static/my-icons/icon2.png', title: '待收货' },
          { image: '/static/my-icons/icon3.png', title: '退款/退货' },
          { image: '/static/my-icons/icon4.png', title: '全部订单' },
        ],
        menuItems: [
          { title: '收获地址' },
          { title: '联系客服' },
          { title: '退出登录' }
        ]
      };
    },
    methods: {
      ...mapMutations('m_user', ['updateToken', 'updateUserInfo', 'updateAddress']),
      handleMenuItemClick(title) {
        if(title === '退出登录') this.$refs.alertDialog.open()
      },
      dialogConfirm(){
        this.updateToken('')
        this.updateUserInfo({})
        this.updateAddress({})
      }
    }
  }
</script>

<style lang="scss">
  .user-profile {
    position: relative;
    height: 100vh;
    background-color: #f4f4f4;
    .profile-header {
      height: 360rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      background-color: #ffbd27;
      .avatar {
        margin: 20rpx 0;
        width: 150rpx;
        height: 150rpx;
        border: 2rpx solid #fff;
        border-radius: 50%;
        box-shadow: 0 2rpx 10rpx black;
      }
      .nickname {
        color: #fff;
      }
    }
    .profile-content {
      width: 100%;
      position: absolute;
      top: 300rpx;
      left: 0;
      .stats-panel {
        margin: 0 20rpx;
        height: 120rpx;
        display: flex;
        justify-content: space-around;
        background-color: #fff;
        border-radius: 10rpx;
        .stat-item {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          font-size: 28rpx;
          .stat-value {
            font-weight: bold;
          }
        }
      }
      .order-panel {
        margin: 20rpx;
        padding: 30rpx 20rpx;
        background-color: #fff;
        border-radius: 10rpx;
        .panel-title {
          font-weight: bold;
        }
      .order-list {
        margin-top: 50rpx;
        display: flex;
        justify-content: space-around;
        .order-item {
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          align-items: center;
          .order-icon {
            width: 73rpx;
            height: 73rpx;
          }
          .order-name {
            margin-top: 10rpx;
            font-size: 28rpx;
          }
        }
      }
      }
      .menu-panel {
        height: 200rpx;
        padding: 30rpx 20rpx;
        margin: 0 20rpx;
        border-radius: 10rpx;
        background-color: #fff;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .menu-item {
          display: flex;
          justify-content: space-between;
          align-items: center;
        }
      }
    }
  }
</style>